SHELL := /bin/bash
BASEDIR = $(shell pwd)
SRC = $(shell find . -type f -name '*.go' -not -path "./vendor/*")

VERSION := v3.0.0

ENV := dev

VersionDir = busygo/pkg/version

# gitHash 当前最新的 commit id, 与 gitCommit 结果相同
gitHash = $(shell git rev-parse HEAD) 
gitBranch = $(shell git rev-parse --abbrev-ref HEAD)
gitTag = $(shell \
            if [ "`git describe --tags --abbrev=0 2>/dev/null`" != "" ]; \
            then \
                git describe --tags --abbrev=0; \
            else \
                git log --pretty=format:'%h' -n 1; \
            fi)
# 与前面的 gitHash 相同
gitCommit = $(shell git log --pretty=format:'%H' -n 1)

gitTreeState = $(shell if git status | grep -q 'clean'; then echo clean; else echo dirty; fi)

buildDate = $(shell TZ=Asia/Shanghai date +%FT%T%z)
# buildDate = $(shell TZ=Asia/Shanghai date +%F\ %T%z | tr 'T' ' ')


ifeq ($(ENV), dev)
    BUILD_FLAGS = -race
endif

ifeq ($(ENV), pro)
    LDFLAGS = -w 
endif


LDFLAGS += -X "${VersionDir}.gitTag=${gitTag}"
LDFLAGS += -X "${VersionDir}.buildDate=${buildDate}"
LDFLAGS += -X "${VersionDir}.gitCommit=${gitCommit}"
LDFLAGS += -X "${VersionDir}.gitTreeState=${gitTreeState}"
LDFLAGS += -X "${VersionDir}.gitBranch=${gitBranch}"
LDFLAGS += -X "${VersionDir}.version=${VERSION}"


all: lint build

build:
    go build  -v -ldflags '$(LDFLAGS)' $(BUILD_FLAGS) -gcflags=all="-N -l"  -o east_money .

lint:
    go fmt ./...
    go vet ./...
    goimports -w .
    # golangci-lint run ./...

# cover:
#     go test ./... -v -short -coverprofile .coverage.txt
#     go tool cover -func .coverage.txt

# 删除指定目录（包括子目录）下所有扩展名为 .sa、.sb、.sc、...、.sz 的隐藏文件（以 . 开头，且紧接着字符 _ 或 .）全部删除
clean:
    rm -f east_money || true
    find . -name "[._]*.s[a-w][a-z]" | xargs -i rm -f {} || true
    rm -rf ./log || true

help:
    @echo "make build   - compile the source code"
    @echo "make clean   - remove binary file and vim swp files"
    @echo "make lint    - run go tool 'fmt', 'vet', 'goimports', 'golangci-lint' "

.PHONY: clean  help